//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The basic module for MapWindow version 6.0
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 3/8/2008 7:19:53 AM
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

using MapWindow.Geometries; // Envelope
using MapWindow.Main; // Progress Handler
using MapWindow.DirectX;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;


namespace MapWindow.DirectX.Data
{
    

    /// <summary>
    /// Texture
    /// </summary>
    public class ModelDX : RenderBaseDX
    {
        #region Events

        
   

        #endregion
       


        #region Private Variables

      

        private IEnvelope _envelope;
        private string _filename;

     

       
        List<Material> _materials;
        List<Texture> _textures;
        List<string> _textureFiles;
       

        private IProgressHandler _progressHandler;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of a mesh layer
        /// </summary>
        public ModelDX()
        {
          
           
        }

        /// <summary>
        /// Creates a texture with the specified file.  A Load method will open the existing texture.  A Save method will overwrite it.
        /// </summary>
        /// <param name="filename">The filename to associate with this texture.</param>
        public ModelDX(string filename)
        {

            _filename = filename;
          
        }

   
       
    
       
        #endregion

        #region Methods







        /// <summary>
        /// This is the internal drawing method that follows the standard checks.  Drawing is evoked
        /// through the Draw3D command, but internally implemented in the DrawPart method.  After
        /// the initial checks, the Draw3D method will cycle the part integer from 0 to NumParts -1.
        /// </summary>
        /// <param name="args">A DrawArgsDX class with the device and camera.</param>
        protected override void OnDraw( DrawArgsDX args)
        {
            Material mat = _materials[args.Part];
           
            args.Device.Material = mat;
            
            if (_textures != null && _textures.Count > args.Part)
            {
                args.Device.SetTexture(0, _textures[args.Part]);
            }
            else
            {
                args.Device.SetTexture(0, null);
            }
        }


        /// <summary>
        /// Disposes any unmanaged memory objects in this 3DModel, like Meshes or textures.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            //if (disposing && components != null)
            //{
            //    components.Dispose();
            //}
            if (_textures != null)
            {
                foreach (Texture text in _textures)
                {
                    text.Dispose();
                }
            }
        }




     



        // This sets up the texture if it hasn't been set up yet.
        /// <summary>
        /// Initializes the model if it hasn't been initialized yet.
        /// </summary>
        public override void Initialize(DrawArgsDX args)
        {


            if (_textureFiles != null)
            {
                foreach (string filename in _textureFiles)
                {
                    string file = System.IO.Path.GetDirectoryName(_filename);
                    file += "\\" + filename;
                    _textures.Add(TextureLoader.FromFile(args.Device, file));
                    System.Diagnostics.Debug.WriteLine("Texture Loaded");
                }
            }
            OnInitialize(args);
            

        }
      
        
       
        /// <summary>
        /// This layer doesn't actually support changing the image.  Instead, this simply updates the worldfile,
        /// but only if the style is georeferenced.
        /// </summary>
        public virtual void Save()
        {
           
        }

        #endregion

        #region Properties

        

        /// <summary>
        /// Obtains an envelope that contains this image
        /// </summary>
        /// <returns>An IEnvelope</returns>
        [Category("Bounds"), Description("Gets an envelope containing this."),
        TypeConverter(typeof(ExpandableObjectConverter))] 
        public override IEnvelope Envelope
        {
            get
            {
                if (_envelope == null)
                {
                    _envelope = new Envelope();
                    _envelope.SetExtents(0, 0, 100, 100);
                }
                return _envelope;
            }
            protected set
            {
                _envelope = value;
            }
        }

        /// <summary>
        /// The string filename representing where data for this model can be found
        /// </summary>
        public string Filename
        {
            get
            {
                return _filename;
            }
            set
            {
                _filename = value;
            }
        }



      


        

        /// <summary>
        /// Gets or sets the list of materials
        /// </summary>
        public virtual List<Material> Materials
        {
            get { return _materials; }
            set { _materials = value; }
        }
 

        /// <summary>
        /// Gets or sets the default progress handler that will be used unless a different one is specificalyl passed as a parameter
        /// </summary>
        public IProgressHandler ProgressHandler
        {
            get { return _progressHandler; }
            set { _progressHandler = value; }
        }




        /// <summary>
        /// Gets or sets the ITexture
        /// </summary>
        public virtual List<Texture> Textures
        {
            get { return _textures; }
            set { _textures = value; }
        }

        /// <summary>
        /// Gets or set the TextureFiles
        /// </summary>
        public virtual List<string> TextureFiles
        {
            get { return _textureFiles; }
            set { _textureFiles = value; }
        }

      

        #endregion


       





    }

}
